﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AAA.Forms.Components
{
    public partial class CheckListPane : PictureBoxX
    {
        private List<CheckItem> _lstItems;
        private string _strCheckedImage;
        private string _strUncheckImage;
        private Image _imgCheckedImage;
        private Image _imgUncheckImage;

        public CheckListPane()
        {
            try
            {
                InitializeComponent();                
                _lstItems = new List<CheckItem>();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "," + ex.StackTrace);
            }
        }

        public void AddItem(string strText)
        {
            AddItem(strText, false);
        }

        public void AddItem(string strText, bool isChecked)
        {
            try
            {
                CheckItem checkItem = new CheckItem(strText, isChecked);
                checkItem.LoadImage(_strUncheckImage, _strCheckedImage);
                checkItem.Font = Font;
                _lstItems.Add(checkItem);
                Container.Add(checkItem);
                Reposition();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "," + ex.StackTrace);
            }
        }

        private void Reposition()
        {
            if (_lstItems.Count == 0)
                return;

            try
            {
                int iHeight = _lstItems[0].Height;
                int iStartX = 20;
                int iStartY = 20;

                for (int i = 0; i < _lstItems.Count; i++)
                {
                    _lstItems[i].Left = iStartY;
                    _lstItems[i].Top = iStartX + iHeight * i;
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "," + ex.StackTrace);
            }
        }

        public void LoadImage(string strUncheck, string strChecked)
        {
            try
            {
                _imgUncheckImage = Image.FromFile(strUncheck);
                _strUncheckImage = strUncheck;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + "," + ex.StackTrace);
            }

            try
            {
                _imgCheckedImage = Image.FromFile(strChecked);
                _strCheckedImage = strChecked;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + "," + ex.StackTrace);
            }
        }

    }
}
